package com.android.gufengmall.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.VideoBean;
import com.android.gufengmall.utils.BitMapConversionUtils;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 视频列表数据适配器
 */
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {

    private Context mContext;
    private List<VideoBean> mVideoBeans;
    private VideoListener mListener;

    public VideoAdapter(Context context, List<VideoBean> videoBeans) {
        mContext = context;
        mVideoBeans = videoBeans;
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VideoViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_video, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        VideoBean videoBean = mVideoBeans.get(position);

        Glide.with(mContext).load(BitMapConversionUtils.stringToBitmap(videoBean.getVideoImg()))
                .dontAnimate().error(R.drawable.icon_error_qqsh).fitCenter().into(holder.mImgVideo);
        holder.mVideoTitle.setText(videoBean.getVideoTitle());
        holder.mImgLike.setImageDrawable(videoBean.getIsLike() ? mContext.getResources().getDrawable(R.drawable.icon_likes)
                : mContext.getResources().getDrawable(R.drawable.icon_like));
        holder.mImgCollect.setImageDrawable(videoBean.getIsCollect() ? mContext.getResources().getDrawable(R.drawable.icon_collects)
                : mContext.getResources().getDrawable(R.drawable.icon_collect));
        holder.mImgLike.setOnClickListener(v -> {
            mListener.onLikeListener(videoBean.getVideoTitle());
        });
        holder.mImgCollect.setOnClickListener(v -> {
            mListener.onCollectListener(videoBean.getVideoTitle());
        });
        holder.mImgVideo.setOnClickListener(v -> {
            mListener.onVideoClick(videoBean.getVideoTitle());
        });
    }

    @Override
    public int getItemCount() {
        return mVideoBeans.size();
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {

        private final ImageView mImgVideo;
        private final TextView mVideoTitle;
        private ImageView mImgLike;
        private ImageView mImgCollect;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);

            mImgVideo = itemView.findViewById(R.id.img_video);
            mVideoTitle = itemView.findViewById(R.id.tv_video_title);
            mImgLike = itemView.findViewById(R.id.img_like);
            mImgCollect = itemView.findViewById(R.id.img_collect);
        }
    }

    public void setOnVideoListener(VideoListener videoListener) {
        mListener = videoListener;
    }

    public interface VideoListener {
        void onVideoClick(String position);

        void onLikeListener(String position);

        void onCollectListener(String position);
    }
}
